from flask import Blueprint
from app.libs.forms import LoginForm, RegisterForm
from app.service.user import UserService
from app.vo import TokenVO

bp_user = Blueprint('user', __name__, url_prefix='/user')


@bp_user.route('/register', methods=['POST'])
def register():
    form = RegisterForm(check_token=False).check_validate()  # 参数校验
    user, token = UserService.register(form)  # 注册
    return TokenVO(
        token=token, msg=f'{user.nickname}(account: {user.account})注册！').response()


@bp_user.route('login', methods=['POST'])
def login():
    form = LoginForm(check_token=False).check_validate()
    user, token = UserService.login(form)  # UserService(form).login() 还是, 当前的好
    return TokenVO(
        token=token, msg=f'{user.nickname}(account: {user.account})登录成功！').response()
